// pages/welfareMall/cart/index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar, //状态栏高度
    CustomBar: app.globalData.CustomBar, //头部nav高度
    backIcon: true, // 返回按钮是否显示
    title: "订单", //页面标题
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    hasData: true,
    selCount: 0,
    totalPrice: 0,
    totalHonor: 0,
    currentPage: 1,
    pageSize: 10,
    totalPage: 1,
    shopcartList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getList()
  },
  getList() {
    wx.showLoading({
      title: '加载中...',
    })
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "currentPage": this.data.currentPage,
      "pageSize": this.data.pageSize
    }
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'mallorder/getOrderList',
      data: data,
      method: 'post',
      success(res) {
        console.log('订单', res)
        const list = that.pushData(res.data.orders)
        wx.hideLoading()
        that.setData({
          cartList: list,
          totalPage: res.data.pageInfo.totalPage
        })
        if (that.data.totalPage) {
          if (that.data.totalPage === that.data.currentPage) {
            that.setData({
              isBottom: true
            })
          }
        }
      }
    })
  },
  pushData(array) {
    let cartList;
    if (this.data.cartList) {
      cartList = this.data.cartList
    } else {
      cartList = []
    }

    array.forEach(item => {
      cartList.push(item)
    })
    return cartList
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const currentPage = this.data.currentPage
    const totalPage = this.data.totalPage
    if (currentPage < totalPage) {
      this.setData({
        currentPage: currentPage + 1
      })
      this.getList()
    } else {
      this.setData({
        isBottom: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})